<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function checkForm(){
		var contractCode = document.getElementById("contractCode");
		var contractName = document.getElementById("contractName");
		var contractDate = document.getElementById("contractDate");
		var updateReason = document.getElementById("updateReason");
		
		var purchaseDescs = document.getElementsByName("purchaseDesc");
		var nums = document.getElementsByName("num");
		var costValues = document.getElementsByName("costValue");
		var discounts = document.getElementsByName("discount");
		var costss = document.getElementsByName("costs");
		var prossProfits = document.getElementsByName("prossProfit");
		
		var currentDate = document.getElementById("currentDate");
		
		if(contractCode.value == ""){
			alert("合同编码<fmt:message key='common.message.empty'/>");
			return false;
		}
		if(contractName.value == ""){
			alert("合同名称<fmt:message key='common.message.empty'/>");
			return false;
		}
		if(contractDate.value == ""){
			alert("签约时间<fmt:message key='common.message.empty'/>");
			return false;
		}
		if(contractDate.value != "" && currentDate.value != ""){
			var d1 = "";
			var d2 = "";
			d1 = (contractDate.value).replace("-","/");
			d2 = (currentDate.value).replace("-","/");
			if(Date.parse(d1) - Date.parse(d2) > 0){
				alert("签约日期不能晚于当前系统日期！");
				return false;
			}
		}
		
		var sign = "<c:out value='${sign}'/>";
		if(sign == '1' && updateReason.value == ""){
			alert("变更原因<fmt:message key='common.message.empty'/>");
			return false;
		}
		
		var sign = 0;
		var totalContractCost = 0;
		for(var i = 0; i < purchaseDescs.length; i++){
			if(purchaseDescs[i].value != "" 
				|| nums[i].value != "" 
				|| costValues[i].value != "" 
				//|| discounts[i].value != "" 
				|| costss[i].value != "" 
				|| prossProfits[i].value != ""){
				
				if(purchaseDescs[i].value == ""){
					alert("采购内容<fmt:message key='common.message.empty'/>");
					return false;
				}
				if(nums[i].value == ""){
					alert("数量<fmt:message key='common.message.empty'/>");
					return false;
				}
				if(costValues[i].value == ""){
					alert("金额<fmt:message key='common.message.empty'/>");
					return false;
				}
				/**if(discounts[i].value == ""){
					alert("折扣率<fmt:message key='common.message.empty'/>");
					return false;
				}*/
				if(costss[i].value == ""){
					alert("成本<fmt:message key='common.message.empty'/>");
					return false;
				}
				if(prossProfits[i].value == ""){
					alert("毛利<fmt:message key='common.message.empty'/>");
					return false;
				}
			}
			
			if(purchaseDescs[i].value != "" 
				&& nums[i].value != "" 
				&& costValues[i].value != "" 
				//|| discounts[i].value != "" 
				&& costss[i].value != "" 
				&& prossProfits[i].value != ""){
				
				if(discounts[i].value == ""){
					alert("折扣率<fmt:message key='common.message.empty'/>");
					return false;
				}
			}
			
			if(purchaseDescs[i].value != "" 
				&& nums[i].value != "" 
				&& costValues[i].value != "" 
				&& discounts[i].value != "" 
				&& costss[i].value != "" 
				&& prossProfits[i].value != ""){
				sign = sign + 1;
				totalContractCost = totalContractCost + Number(costValues[i].value);
			}
		}
		if(sign <= 0){
			alert("合同明细不能为空，请追加合同明细！");
			return false;
		}
		if(totalContractCost < 0.01){
			alert("合同金额不能小于0!");
			return false;
		}
		
		return true;
	}

	function back(){
		var uri = "./contract.do";
		var sign = "<c:out value='${sign}'/>";
		if(sign == "1"){//返回到执行中合同
			uri = "./contract.do";
		}else if(sign == "2"){//返回到待审的合同
			uri = "./contract.do?action=preWaitAduitContract";
		}else if(sign == "3"){//返回到退回的合同
			uri = "./contract.do?action=preReturnContract";
		}
		window.location.href = uri;
		window.location.target = "main";
	}
	
	function personDialog(){
		var url = "./contract.do?action=selectSalePerson";
		var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取销售代表id和销售代表名称
			var salePersonId = "";
			var salePerson = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				salePersonId = tStr[0];
				salePerson = tStr[1];
			}
			document.getElementById("salePersonId").value = salePersonId;										
			document.getElementById("salePerson").value = salePerson;
		}
	}
	
	function productDialog(){
		var url = "selectProduct.html";
		var dlgParam = "dialogHeight:400px;dialogWidth:500px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		window.showModalDialog(url,"",dlgParam);
	}
	
	function save(){
		if(checkForm()){
			document.updateContractForm.action = "./contract.do?action=updateContractProName";
			document.updateContractForm.target = "main";
			document.updateContractForm.submit();
			
			document.getElementById("saveBtn").href = "#";
		}
	}
	
	function getCostsMain(obj,rowNum){
		onlyNumFloat(obj);
		getCosts(rowNum);
	}
	//计算成本
	function getCosts(rowNum){
		rowNum = Number(rowNum) - 1;
		var costValues = document.getElementsByName("costValue");//折扣后的金额
		//var discounts = document.getElementsByName("discount");//折扣率
		var costss = document.getElementsByName("costs");//成本
		var prossProfits = document.getElementsByName("prossProfit");//毛利
		if(costValues.length > 0 
			&& costss.length > 0 && prossProfits.length > 0){
			if(costValues[rowNum].value != ""){
				document.getElementsByName("costs")[rowNum].value = 
					Number(costValues[rowNum].value) - Number(prossProfits[rowNum].value);
			}
		}
	}
	
	function getProssProfitMain(obj,rowNum){
		onlyNumFloat(obj);
		getProssProfit(rowNum);
	}
	//计算毛利
	function getProssProfit(rowNum){
		rowNum = Number(rowNum) - 1;
		var costValues = document.getElementsByName("costValue");//折扣后金额
		//var discounts = document.getElementsByName("discount");//折扣率
		var costss = document.getElementsByName("costs");//成本
		var prossProfits = document.getElementsByName("prossProfit");//毛利
		if(costValues.length > 0 
			&& costss.length > 0 && prossProfits.length > 0){
			if(costValues[rowNum].value != ""){
				document.getElementsByName("prossProfit")[rowNum].value = 
					Number(costValues[rowNum].value) - Number(costss[rowNum].value);
			}
		}
	}
	
	function getCostsAndProssProfitMain(rowNum){
		getCosts(rowNum);
		getProssProfit(rowNum);
	}
	function getCostsAndProssProfit(obj,rowNum){
		onlyNumFloat(obj);
		getCosts(rowNum);
		getProssProfit(rowNum);
	}
	
	function deleteAdded(){
		var currentValue = document.all.addedId.value;
		if(currentValue != ""){
			document.updateContractForm.action = "./contract.do?action=deleteAdded&addedId="+currentValue;
			document.updateContractForm.target = "main";
			document.updateContractForm.submit();
		}else{
			alert("请选择要删除的附件！");
		}
	}
	
	//追加行start
	function addRow(tableId){//增加tab的一行	
		try{				
			var objT =  document.getElementById(tableId);//通过ID 得到对象
			if(objT==null){	alert("没有可操作的表！");return;	}
			if(objT.rows.length<1){ alert("没有表格标题行"); return;}
			
			var newTr = objT.insertRow(-1);//插入一行
			newTr.className = "table_style";	//为新行赋样式
			var colsLength = objT.rows(0).cells.length;  //表中列总数 
			var newTd ;
			var hiddenObj;		
			var insertTdHtml = prepareInsertHtml(newTrNum);//获得新增行各个TD的HTML代码
			var i=0;
			for(i=0;i<colsLength;i++){
				newTd = newTr.insertCell();
				newTd.innerHTML = insertTdHtml[i];
				newTd.align="left";				
			}
			newTrNum++;//表格总行数加1
		}catch(Exception){}
	}
	
	//增加行所需代码
	var newTrNum = 0;//表格最大行号，只增不减
	var insertHtml = new Array();//用于插入新记录的HTML
	function prepareInsertHtml(num){
		insertHtml[0] = "<input id='purchaseDesc' name='purchaseDesc' class='input_percent' type='text'/>";
		insertHtml[0] += "<input id='unit' name='unit' type='hidden'/>";	
		insertHtml[0] += "<input id='price' name='price' type='hidden'/>";
		//insertHtml[1] = "<input id='unit' name='unit' type='hidden'/>";	
		//insertHtml[2] = "<input id='price' name='price' type='hidden'/>";	
		insertHtml[1] = "<input id='num' name='num' class='input_percent' type='text' onkeyup='onlyNumber(this)'/>";
		insertHtml[2] = "<input id='discount' name='discount' class='input_percent' type='text' value='100' onkeyup='getCostsAndProssProfit(this,"+num+")' onblur='getCostsAndProssProfitMain("+num+")'/>";	
		insertHtml[3] = "<input id='costValue' name='costValue' class='input_percent' type='text' onkeyup='getCostsAndProssProfit(this,"+num+")' onblur='getCostsAndProssProfitMain("+num+")'/>";
		insertHtml[4] = "<input id='costs' name='costs' class='input_percent' type='text' onkeyup='getProssProfitMain(this,"+num+")' onblur='getProssProfit("+num+")'/>";
		insertHtml[5] = "<input id='prossProfit' name='prossProfit' class='input_percent' type='text' onkeyup='getCostsMain(this,"+num+")' onblur='getCosts("+num+")'/>";
		//insertHtml[6] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img class='btn_img' src='./images/a_add.gif' onclick='productDialog()' title='选择产品'>";
		return insertHtml;
	}
	//追加行end
	
	function initPage(){
		newTrNum = table1.rows.length;//表格最大行号，只增不减
		
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.updateSuccess'/>");
		}
		if("<c:out value='${failResult}'/>" == 3){
			alert("修改失败：上传文件大小不能超过10M!");
		}
		if("<c:out value='${repeatResult}'/>" == 1){
			alert("修改失败：合同编码已存!");
		}
		if("<c:out value='${uploadResult}'/>" == 4){
			alert("修改失败：请勿重复上传附件!");
		}
		if("<c:out value='${deleteResult}'/>" == 1){
			alert("删除附件成功！");
		}
		
		document.getElementById("saveBtn").href = "javascript:save();";
	}
-->
</script>
</head>
<body onload="initPage()">
<form name="updateContractForm" action="" method="post" enctype="multipart/form-data">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">合同变更</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<button class="btn_submit" onclick="save()" hidefocus="true">提交</button>&nbsp;&nbsp;
							<button class="btn_back" onclick="back()" hidefocus="true">取消并返回</button>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title">客户名称：</td>
									<td>
										${contract.customerName}
										<input id="customerName" name="customerName" type="hidden" value="${contract.customerName}"/>
										<input id="customerId" name="customerId" type="hidden" value="${contract.customerId}"/>
									</td>
									<td class="title">合同名称：</td>
									<td>
										<input id="contractName" name="contractName" class="input_long" type="text" value="${contract.contractName}" readonly="readonly"/>
										<input id="id" name="id" type="hidden" value="${contract.id}"/>
									</td>
								</tr>
								<tr>
									<td class="title">合同编号：</td>
									<td class=""><input id="contractCode" name="contractCode" class="input_long" type="text" value="${contract.contractCode}" readonly="readonly"/></td>
									<td class="title">签约时间：</td>
									<td class="">
										<input id="contractDate" name="contractDate" class="input_long" type="text" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" 
											value="<fmt:formatDate value='${contract.contractDate}' pattern='yyyy-MM-dd'/>" disabled="disabled"/>
									</td>
								</tr>
								<tr>
									<td class="title">销售代表：</td>
									<td class="">
										<input id="salePerson" name="salePerson" type="text" value="${contract.salePerson}" readonly="readonly" 
											class="<c:choose><c:when test='${isViewAllContract == 1}'>input_long</c:when><c:otherwise>input_long_noborder</c:otherwise></c:choose>"/>&nbsp;
										<input id="salePersonId" name="salePersonId" type="hidden" value="${contract.salePersonId}"/>
										<c:if test="${isViewAllContract == 1}">
										<input class="btn_select" type="button" value="" onclick="personDialog()" disabled="disabled"/>
										</c:if>
									</td>
									<td class="title">制单日期：</td>
									<td class="">
										<fmt:formatDate value="${contract.createDate}" pattern="yyyy-MM-dd"/>
										<input id="createDate" name="createDate" type="hidden" 
											value="<fmt:formatDate value='${contract.createDate}' pattern='yyyy-MM-dd'/>"/>
									</td>
								</tr>
								<tr>
									<td class="title">备注：</td>
									<td class="" colspan="3">
										<textarea id="contractDesc" name="contractDesc" class="textarea_remark" wrap="hard">${contract.contractDesc}</textarea>
									</td>
								</tr>
								<tr>
									<td rowspan="2" style="height: 60px;" class="title"><font style="font-size: 10px;">(附件 &lt; 10M)</font>&nbsp;&nbsp;上传附件：</td>
									<td colspan="3">
										<select id="addedId" name="addedId" style="width: 86%" disabled="disabled">
											<c:forEach var="systemFile" items="${contractAddeds}">
											<option value="${systemFile.id}">附件名称：${systemFile.fileName}，上传者：${systemFile.person}，上传时间：<fmt:formatDate value="${systemFile.uploadDate}" pattern="yyyy-MM-dd"/></option>
											</c:forEach>
										</select>&nbsp;
										<input class="" type="button" value=" 删 除 " onclick="deleteAdded()" disabled="disabled">
									</td>
								</tr>
								<tr>
									<td colspan="4">
										<input id="contents" name="contents" type="file" style="width: 96%" disabled="disabled"/>
									</td>
								</tr>
								<c:if test="${sign == 1}">
								<tr>
									<td class="title">变更原因：</td>
									<td class="" colspan="3"><textarea id="updateReason" name="updateReason" class="textarea_remark" wrap="hard">${contract.updateReason}</textarea></td>
								</tr>
								</c:if>
								<tr>
									<td class="title">审核人：</td>
									<td>
										${contract.auditPerson}
										<input id="auditPerson" name="auditPerson" type="hidden" value="${contract.auditPerson}"/>
									</td>
									<td class="title">审核日期：</td>
									<td>
										<fmt:formatDate value="${contract.auditDate}" pattern="yyyy-MM-dd HH:mm"/>
										<input id="auditDate" name="auditDate" type="hidden" 
											value="<fmt:formatDate value='${contract.auditDate}' pattern='yyyy-MM-dd'/>"/>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title_top" width="60">合同标的</td>
									<td class="title_top"><button class="btn_append" onclick="addRow('table1')" hidefocus="true" disabled="disabled">新增案件</button></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table id="table1" class="table_style">
								<tr>
									<td class="title">商品名称</td>
									<!-- 
									<td class="title4" width="8%">单位</td>
									<td class="title4" width="12%">单价(元)</td>
									 -->
									<td class="title" width="8%">数量</td>
									<td class="title" width="12%">折扣率(%)</td>
									<td class="title" width="12%">折扣后的金额(元)</td>
									<td class="title" width="12%">成本(元)</td>
									<td class="title" width="12%">毛利(元)</td>
									<!-- 
									<td class="title4" width="8%">操作</td>
									 -->
								</tr>
								<%int i = 1; %>
								<c:forEach var="contractDetail" items="${contractDetails}">
								<tr>
									<td>
										<input id="purchaseDesc" name="purchaseDesc" class="input_percent" type="text" value="${contractDetail.purchaseDesc}"/>
										${contractDetail.unit}<input id="unit" name="unit" type="hidden" value="${contractDetail.unit}"/>
										${contractDetail.price}<input id="price" name="price" type="hidden" value="${contractDetail.price}"/>
									</td>
									<!-- 
									<td>${contractDetail.unit}<input id="unit" name="unit" type="hidden" value="${contractDetail.unit}"/></td>
									<td>${contractDetail.price}<input id="price" name="price" type="hidden" value="${contractDetail.price}"/></td>
									 -->
									<td><input id="num" name="num" class="input_percent" type="text" value="${contractDetail.num}" onkeyup="onlyNumber(this)" readonly="readonly"/></td>
									<td>
										<input id="discount" name="discount" class="input_percent" type="text" value="${contractDetail.discount}" 
											onkeyup="getCostsAndProssProfit(this,<%=i%>)" onblur="getCostsAndProssProfitMain(<%=i%>)" readonly="readonly"/>
									</td>
									<td>
										<input id="costValue" name="costValue" class="input_percent" type="text" value="${contractDetail.costValue}" 
											onkeyup="getCostsAndProssProfit(this,<%=i%>)" onblur="getCostsAndProssProfitMain(<%=i%>)" readonly="readonly"/>
									</td>
									<td>
										<input id="costs" name="costs" class="input_percent" type="text" 
											value="${contractDetail.costs}" onkeyup="getProssProfitMain(this,<%=i%>)" onblur="getProssProfit(<%=i%>)" readonly="readonly"/>
									</td>
									<td>
										<input id="prossProfit" name="prossProfit" class="input_percent" type="text" 
											value="${contractDetail.prossProfit}" onkeyup="getCostsMain(this,<%=i%>)" onblur="getCosts(<%=i%>)" readonly="readonly"/>
									</td>
									<!-- 
									<td><img class="btn_img" src="./images/a_add.gif" onclick="productDialog()" title="选择产品"></td>
									 -->
									<%i = i + 1; %>
								</tr>
								</c:forEach>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<input id="sign" name="sign" type="hidden" value="${sign}"/>
	<input id="currentDate" name="currentDate" type="hidden" value="<fmt:formatDate value='<%=new java.util.Date()%>' pattern="yyyy-MM-dd"/>"/>
</form>
</body>
</html>